package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.dal.entitys.Stock;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: 叶熠彬
 * @Description: 扣减库存处理器
 * @date: 2022/7/29 21:51
 */
@Component
@Slf4j
public class SubStockHandler extends AbstractTransHandler {

    @Autowired
    StockMapper stockMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    @Transactional
    public boolean handle(TransHandlerContext context) {

        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        List<Long> buyProductIds = new ArrayList<>();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            Long productId = cartProductDto.getProductId();
            buyProductIds.add(productId);
        }

        createOrderContext.setBuyProductIds(buyProductIds);

        // 锁定库存
        List<Stock> stocksForUpdate = stockMapper.findStocksForUpdate(buyProductIds);
        if (CollectionUtils.isEmpty(stocksForUpdate)) {
            throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(), "库存未初始化");
        }
        if (stocksForUpdate.size() != buyProductIds.size()) {
            throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(), "部分商品库存未初始化");
        }

        for (CartProductDto cartProductDto : cartProductDtoList) {
            Long productId = cartProductDto.getProductId();
            Long productNum = cartProductDto.getProductNum();
            Stock selectStock = stockMapper.selectStock(productId);
            // productNum不能超出库存总数
            if (productNum > selectStock.getStockCount()) {
                throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(), cartProductDto.getProductName() + "库存不足");
            }

            // 添加冻结库存，扣减库存
            Stock stock = new Stock();
            stock.setItemId(productId);
            stock.setLockCount(productNum.intValue());
            stock.setStockCount(-productNum);

            stockMapper.updateStock(stock);
        }

        return true;
    }

    @Transactional
    public boolean handle2(TransHandlerContext context) {
        // 向下转型
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        // 获取createOrderContext中的商品id list 请求中未赋值
        List<Long> productIds = createOrderContext.getBuyProductIds();
        Stock updateStock = new Stock();
        updateStock.setItemId(productIds.get(0));
        updateStock.setLockCount(1);
        updateStock.setStockCount(-1L);
        stockMapper.updateStock(updateStock);

        return true;
    }
}